﻿using System;
using SampleApp.Domain;
using SampleApp.Data;
using COM.Service;

namespace SampleApp.Service
{
    public class AppLogService : IAppLogService
    {
        private readonly IAppLogRepository appLogRepository;
        private readonly IConfigService configService;
        public AppLogService(IAppLogRepository appLogRepository, IConfigService configService)
        {
            this.appLogRepository = appLogRepository;
            this.configService = configService;
        }

        public void Log(string application, string category, string msg)
        {
            var log = new ApplicationLog()
            {
                RowID = Guid.NewGuid().ToString(),
                Application = application,
                Category = category,
                Infomation = msg,
                RecordDate = DateTime.Now,
            };
            this.appLogRepository.Add(log);
            this.appLogRepository.Save();
        }

        public void LogErrorMsg(string errorMsg)
        {
            this.Log(this.configService.DeployHostName, "Error", errorMsg);
        }

        public void LogInfoMsg(string infoMsg)
        {
            this.Log(this.configService.DeployHostName, "Info", infoMsg);
        }

        public void LogError(string format, params object[] args)
        {
            this.LogErrorMsg(args.Length == 0 ? format : string.Format(format, args));
        }

        public void LogInfo(string format, params object[] args)
        {
            this.LogInfoMsg(args.Length == 0 ? format : string.Format(format, args));
        }



    }
}
